package net.zhengxinyun.performance.service.indicators;

import net.zhengxinyun.performance.entity.TownEntity;
import net.zhengxinyun.performance.entity.TownIndicatorsEntity;
import net.zhengxinyun.performance.filter.TownFilter;
import net.zhengxinyun.performance.mapper.Indicators.TownMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @Auther: hyp
 * @Date: 2018/10/29 18:25
 */
@Service
public class TownServiceImpl implements TownService {

    private TownMapper townMapper;

    @Autowired
    public TownServiceImpl(TownMapper townMapper) {
        this.townMapper = townMapper;
    }

    @Override
    public int delete(TownEntity townEntity) {
        return townMapper.deleteByPrimaryKey(townEntity.getId());
    }

    @Override
    public int update(TownEntity townEntity) {
        return townMapper.updateByPrimaryKey(townEntity);
    }

    @Override
    public String insert(TownIndicatorsEntity townIndicatorsEntity,List<TownEntity> townList) {
        Long lastId = townIndicatorsEntity.getId();
        for (TownEntity townEntity : townList) {
            townEntity.setIndexId(lastId.toString());
            long a = townMapper.insert(townEntity);
            if (a != 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "添加失败,请联系管理员";
            }
        }
        return "添加成功";
    }

    @Override
    public List<TownFilter> query(TownFilter townFilter) {
        return townMapper.query(townFilter);
    }
}
